//
//  SecondDetailTableViewController.m
//  bh
//
//  Created by lanou on 15/12/21.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "SecondDetailTableViewController.h"
#import "HeaderSecondDetailView.h"
#import "Picture.h"
#import "FirstDatailTableViewCell.h"
#import "CollectionViewCell.h"
#import "SecondDetailTableViewCell.h"
#import "SecondDetailCollectionViewCell.h"
#import "ThirdTableViewCell.h"
#import "FourTableViewCell.h"

@interface SecondDetailTableViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)Photos *photos1;
@property (nonatomic,strong)NSMutableArray *photosArray;
@property (nonatomic,strong)HeaderSecondDetailView *headerView;
@property (nonatomic,strong)NSMutableArray *pictureArray;
@property (nonatomic,strong)FirstDatailTableViewCell *firstCell;
@property (nonatomic,strong)SecondDetailTableViewCell *secondCell;
@property (nonatomic,strong)NSMutableArray *commentsArray;

@property (nonatomic,strong)UITableView *NoteTableView;



@end

@implementation SecondDetailTableViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [self.navigationController setNavigationBarHidden:YES];
}

- (NSMutableArray *)photosArray{
    if (_photosArray == nil) {
        _photosArray = [[NSMutableArray alloc]init];
    }
    return _photosArray;
}

- (NSMutableArray *)pictureArray{
    if (_pictureArray == nil) {
        _pictureArray =[[NSMutableArray alloc]init];
    }
    return _pictureArray;
}

- (NSMutableArray *)commentsArray{
    if (_commentsArray == nil) {
        _commentsArray = [[NSMutableArray alloc]init];
    }
    return _commentsArray;
}



//第二个界面的画册点击这个视图的单元格进入到详情页面的网址(接受上一个界面传过来的col_id)
- (void)jsonWithSecondDetail{
    
    NSString *url = nil;
    if (self.string == nil) {
         url = [NSString stringWithFormat:kSecondPhotoDeatilUrl,self.photos.col_id];
    }else{
        url = [NSString stringWithFormat:kSecondPhotoDeatilUrl,self.string];
        
    }
    
   
    [LORequestManger GET:url success:^(id response) {
        
        //对第一个单元格的内容的解析
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *collection_photos2 = dic[@"data"][@"collection_photos2"];
        NSArray *array = collection_photos2[@"photos"];
        for (NSDictionary *dict in array) {
            NSArray *array1 = dict[@"value"];
            for (NSDictionary *valueDic in array1) {
                Picture *picture = [Picture shareWithDictionary:valueDic];
                [self.photosArray addObject:picture];
            }
        }
        //对第二个单元格的内容的解析
        NSDictionary *collection_like_users = dic[@"data"][@"collection_liked_users"];
        NSArray *likeArray = collection_like_users[@"users"];
        for (NSDictionary *likeDic in likeArray) {
            Picture *picture1 = [Picture shareWithDictionary:likeDic];
            [self.pictureArray addObject:picture1];
        }
        //对第三个单元格的内容的解析
        NSDictionary *collection_comments = dic[@"data"][@"collection_comments"];
        NSArray *commentsArray = collection_comments[@"comments"];
        for (NSDictionary *comments in commentsArray) {
            Picture *picture2 = [Picture shareWithDictionary:comments];
            [self.commentsArray addObject:picture2];
            NSLog(@"----------%@",self.commentsArray);
        }
        NSDictionary *info = dic[@"data"][@"collection_info"][@"info"];
        self.photos1 = [Photos shareWithDictionary:info];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self jsonWithSecondDetail];
    
    //使navigationController.navigationBar 消失
//    self.navigationController.navigationBar.alpha = 0;
    
    NSLog(@"%@",self.photos.col_id);
    self.headerView =[[HeaderSecondDetailView alloc]initWithFrame:(CGRectMake(0, 0, kWidth, 300))];
    if (self.photos != nil) {
       
        self.headerView.photos = self.photos;
        self.headerView.areaLabel.text =self.photos1.title_sub_3;
    }else{
        
        [self.headerView.HeaderImageView setImageWithURL:[NSURL URLWithString:self.picString]];
    }
   
    
    
    
    [self.headerView.backButton addTarget:self action:@selector(backSecondViewController) forControlEvents:(UIControlEventTouchUpInside)];
    self.tableView.tableHeaderView = self.headerView;
    
    //第一种单元格
    [self.tableView registerClass:[FirstDatailTableViewCell class] forCellReuseIdentifier:@"firstCell"];
    //第二种单元格
    [self.tableView registerClass:[SecondDetailTableViewCell class] forCellReuseIdentifier:@"secondCell"];
    //第三种单元格
    [self.tableView registerClass:[ThirdTableViewCell class] forCellReuseIdentifier:@"thirdCell"];
    //第四种单元格
    
    //创建分段控制器
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:@[@"图片",@"日记",@"清单"]];
    segmentedControl.frame = CGRectMake(0, 240, kWidth, 40);
    segmentedControl.tintColor = [UIColor orangeColor];
    segmentedControl.selectedSegmentIndex = 0;
    [segmentedControl addTarget:self action:@selector(changeValueAction:) forControlEvents:(UIControlEventValueChanged)];
    
    
    
    [self.headerView addSubview:segmentedControl];
    
    //日记界面的添加
    self.NoteTableView = [[UITableView alloc]initWithFrame:(CGRectMake(0, 330, kWidth, self.tableView.frame.size.height))];
    [self.NoteTableView registerClass:[SecondDetailTableViewCell class] forCellReuseIdentifier:@"noteSecondCell"];
    [self.NoteTableView registerClass:[ThirdTableViewCell class] forCellReuseIdentifier:@"noteThirdCell"];
    self.NoteTableView.delegate =self;
    self.NoteTableView.dataSource = self;
    self.NoteTableView.backgroundColor = [UIColor cyanColor];
    
    

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

//分段控制器方法的实现
- (void)changeValueAction:(UISegmentedControl *)segmentedControl{
    
    switch (segmentedControl.selectedSegmentIndex) {
        case 0:
            [self.NoteTableView removeFromSuperview];
            break;
        case 1:
            NSLog(@"123");
            [self.tableView addSubview:self.NoteTableView];
            [self.NoteTableView reloadData];
            break;
        default:
            break;
    }
}







//返回backButton的实现方法
- (void)backSecondViewController{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    if (tableView == self.tableView) {
         return 4;
    }else{
        NSLog(@"1111111noteTableView");
        return 3;
    }
   
}      

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    if (tableView == self.tableView) {
         if (section == 2) {
        if (self.commentsArray == nil) {
            return 1;
        }else{
            NSLog(@"这个数组有值");
        return [self.commentsArray count];
        }
    }else{
        return 1;
    }

    }else{
        if (section == 1) {
            if (self.commentsArray == nil) {
                return 1;
            }else{
                NSLog(@"这个数组有值");
                return [self.commentsArray count];
            }
        }else{
            NSLog(@"222222noteTableView");

            return 1;
        }
 
      
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.tableView) {
        if (indexPath.section == 0) {
            self.firstCell = [tableView dequeueReusableCellWithIdentifier:@"firstCell"];
            [ self.firstCell.FirstCollectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
            self.firstCell.FirstCollectionView.delegate =self;
            self.firstCell.FirstCollectionView.dataSource =self;
            [ self.firstCell.FirstCollectionView reloadData];
            
            NSLog(@"%@",self.photosArray);
            return  self.firstCell;
        }else if(indexPath.section == 1){
            self.secondCell = [tableView dequeueReusableCellWithIdentifier:@"secondCell"];
            [self.secondCell.SecondCollectionView registerClass:[SecondDetailCollectionViewCell class] forCellWithReuseIdentifier:@"cell2"];
            self.secondCell.SecondCollectionView.delegate =self;
            self.secondCell.SecondCollectionView.dataSource =self;
            [self.secondCell.SecondCollectionView reloadData];
            return self.secondCell;
        }else if(indexPath.section == 2){
            ThirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"thirdCell"];
            if (self.commentsArray != nil) {
                cell.picture = self.commentsArray[indexPath.row];
                return cell;
            }else{
                NSLog(@"快来抢第一个沙发…………");
                return cell;
            }
        }else{
            static NSString *identifier = @"cell1";
            
            FourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell == nil) {
                cell = [[FourTableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
            }
            
            return cell;
        }

    }else{
        
        if(indexPath.section == 0){
            self.secondCell = [tableView dequeueReusableCellWithIdentifier:@"noteSecondCell"];
            [self.secondCell.SecondCollectionView registerClass:[SecondDetailCollectionViewCell class] forCellWithReuseIdentifier:@"cell2"];
            self.secondCell.SecondCollectionView.delegate =self;
            self.secondCell.SecondCollectionView.dataSource =self;
            [self.secondCell.SecondCollectionView reloadData];
            return self.secondCell;
        }else if(indexPath.section == 1){
            ThirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"noteThirdCell"];
            if (self.commentsArray != nil) {
                cell.picture = self.commentsArray[indexPath.row];
                return cell;
            }else{
                NSLog(@"快来抢第一个沙发…………");
                return cell;
            }
        }else{
            static NSString *identifier = @"cell1";
            
            FourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell == nil) {
                cell = [[FourTableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
            }
            return cell;
        }
    }
       // Configure the cell...
}

//collectionView 的代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return [self.photosArray count];
    }else{
        return [self.pictureArray count];
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.firstCell.FirstCollectionView) {
         CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    cell.picture = self.photosArray[indexPath.item];
    
        
        
        return cell;
    }else{
        SecondDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
        if (self.pictureArray.count == 0) {
            
        }else{
            
            cell.picture = self.pictureArray[indexPath.item];
            
            cell.pictureView.layer.masksToBounds = YES;
            cell.pictureView.layer.borderWidth = 2;
            cell.pictureView.layer.cornerRadius = cell.pictureView.bounds.size.width / 2;
            cell.pictureView.layer.borderColor = [UIColor clearColor].CGColor;
            
            
            
        }
        
        return cell;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableView) {
        if (indexPath.section == 0) {
        return 950;
    }else if (indexPath.section == 1){
        if (self.pictureArray.count == 0) {
            return 10;
        }else{
        return 160;
        }
    }else{
        return 60;
    }
    }else{
         if (indexPath.section == 0){
            return 160;
        }else{
            return 60;
        }
 
        
    }
    
    
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (tableView == self.tableView) {
        if (section == 2) {
            UIView *secondView =[[UIView alloc]initWithFrame:(CGRectMake(0, 0, kWidth, 30))];
            UILabel *label = [[UILabel alloc]initWithFrame:(CGRectMake(5, 0, 100, 30))];
            label.text = @"评论";
            label.textColor = [UIColor orangeColor];
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
            button.frame = CGRectMake(kWidth - 75, 0, 50, 30);
            [button setTitle:@"更多" forState:(UIControlStateNormal)];
            [button addTarget:self action:@selector(turnToOtherLikePeople) forControlEvents:(UIControlEventTouchUpInside)];
            secondView.backgroundColor = [UIColor cyanColor];
            [secondView addSubview:button];
            [secondView addSubview:label];
            return secondView;
            
        }else if(section == 1){
            
            UIView *firstView =[[UIView alloc]initWithFrame:(CGRectMake(0, 0, kWidth, 30))];
            UILabel *label = [[UILabel alloc]initWithFrame:(CGRectMake(5, 0, 100, 30))];
            label.text = @"赞过的人";
            label.textColor = [UIColor orangeColor];
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
            button.frame = CGRectMake(kWidth - 75, 0, 50, 30);
            [button setTitle:@"更多" forState:(UIControlStateNormal)];
            [button addTarget:self action:@selector(turnToOtherLikePeople) forControlEvents:(UIControlEventTouchUpInside)];
            firstView.backgroundColor = [UIColor cyanColor];
            [firstView addSubview:button];
            [firstView addSubview:label];
            return firstView;
        }else{
            
            return nil;
            
            
            
        }
    }else{
        
        if (section == 1) {
            UIView *secondView =[[UIView alloc]initWithFrame:(CGRectMake(0, 0, kWidth, 30))];
            UILabel *label = [[UILabel alloc]initWithFrame:(CGRectMake(5, 0, 100, 30))];
            label.text = @"评论";
            label.textColor = [UIColor orangeColor];
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
            button.frame = CGRectMake(300, 0, 50, 30);
            [button setTitle:@"更多" forState:(UIControlStateNormal)];
            [button addTarget:self action:@selector(turnToOtherLikePeople) forControlEvents:(UIControlEventTouchUpInside)];
            secondView.backgroundColor = [UIColor cyanColor];
            [secondView addSubview:button];
            [secondView addSubview:label];
            return secondView;
            
        }else if(section == 0){
            
            UIView *firstView =[[UIView alloc]initWithFrame:(CGRectMake(0, 0, kWidth, 30))];
            UILabel *label = [[UILabel alloc]initWithFrame:(CGRectMake(5, 0, 100, 30))];
            label.text = @"赞过的人";
            label.textColor = [UIColor orangeColor];
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
            button.frame = CGRectMake(300, 0, 50, 30);
            [button setTitle:@"更多" forState:(UIControlStateNormal)];
            [button addTarget:self action:@selector(turnToOtherLikePeople) forControlEvents:(UIControlEventTouchUpInside)];
            firstView.backgroundColor = [UIColor cyanColor];
            [firstView addSubview:button];
            [firstView addSubview:label];
            return firstView;
        }else{
            
            return nil;
            
            
            
        }
 
        
    }
   
}

- (void)turnToOtherLikePeople{
    NSLog(@"dina45545");
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
